<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestGameManager extends PHPUnit_Framework_TestCase {
	function test_game_get_games() {
		$expected_game_id_1 = 321;
		$expected_game_id_2 = 432;
		$expected_game_type_1 = 'game type 1';
		$expected_game_type_2 = 'game type 2';
		$expected_result_1 = array(
				'game_id' => $expected_game_id_1, 
				'game_type' => $expected_game_type_1); 
		$expected_result_2 = array(
				'game_id' => $expected_game_id_2, 
				'game_type' => $expected_game_type_2); 
		$expected_sql = "sql";

		$manager = new GameManager();
		
		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
						array(
								array(), 
								"GAME.*, GAME_TYPE.*"), 
						array(
								"(GAME INNER JOIN GAME_OF_TYPE ON GAME.game_id = GAME_OF_TYPE.game_id) " . 
								"INNER JOIN GAME_TYPE ON GAME_OF_TYPE.game_type_id = GAME_TYPE.game_type_id", 
								"DUMMY"), 
						 1, 
						 "game_type ASC, name ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->exactly(3))
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->onConsecutiveCalls($expected_result_1, $expected_result_2, null));
		$manager->set_db_accessor($mock_db_access);

		$actual = $manager->game_get_games();
		$expected_result_1['types'] = array($expected_game_type_1);
		$expected_result_2['types'] = array($expected_game_type_2);
		$this->assertEquals($expected_result_1, $actual[$expected_game_id_1]);
		$this->assertEquals($expected_result_2, $actual[$expected_game_id_2]);
	}

	function test_game_get_games_with_doubles() {
		$expected_game_id_1 = 321;
		$expected_game_id_2 = 321;
		$expected_game_type_1 = 'game type 1';
		$expected_game_type_2 = 'game type 2';
		$expected_result_1 = array(
				'game_id' => $expected_game_id_1, 
				'game_type' => $expected_game_type_1); 
		$expected_result_2 = array(
				'game_id' => $expected_game_id_2, 
				'game_type' => $expected_game_type_2); 
		$expected_sql = "sql";

		$manager = new GameManager();
		
		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(
						array(
								array(), 
								"GAME.*, GAME_TYPE.*"), 
						array(
								"(GAME INNER JOIN GAME_OF_TYPE ON GAME.game_id = GAME_OF_TYPE.game_id) " . 
								"INNER JOIN GAME_TYPE ON GAME_OF_TYPE.game_type_id = GAME_TYPE.game_type_id", 
								"DUMMY"), 
						 1, 
						 "game_type ASC, name ASC")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->exactly(3))
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->onConsecutiveCalls($expected_result_1, $expected_result_2, null));
		$manager->set_db_accessor($mock_db_access);

		$actual = $manager->game_get_games();
		$expected_result_1['types'] = array($expected_game_type_1, $expected_game_type_2);
		$this->assertEquals($expected_result_1, $actual[$expected_game_id_1]);
	}

}

?>
